(function () {
    window.kityFormulaDialog = null;

    var kityformula = (function () {
        'use strict';
    
        var global = tinymce.util.Tools.resolve('tinymce.PluginManager');


    
        var kityIframe = null;
        window.addEventListener('message', function (event) {
            // console.log('msg-parent:::', event.data)
            var data = event.data;
    
            if(data.cmd === 'initKity') {
                var img = tinymce.activeEditor.selection.getNode();
                var imgLatex = img && $(img).attr('data-latex');
                kityIframe = event.source;
                event.source.postMessage({
                    type: 'render',
                    content: imgLatex
                }, '*')
                return;
            }
            if(data.mceAction === 'insertContent') {
                tinymce.activeEditor.execCommand('mceInsertContent', false, data.content);
                kityFormulaDialog.close();
                return;
            }
        });
        

        var open = function (editor) {
          kityFormulaDialog = editor.windowManager.open({
            title: '公式',
            editor: editor,
            width: 785,
            height: 386,
            url: tinymce.baseURL + '/plugins/kityformula/kityFormulaDialog.html',
            id: 'kityFormulaDialog',
            name: 'kityFormulaDialog',
            buttons: [
              {
                name: 'closeButton',
                text: 'Cancel',
                onclick: function (e) {
                  e.control.parent().parent().close();
                }
              },
              {
                name: 'submitButton',
                text: 'Save',
                subtype: 'primary',
                onclick: function (e) {
                  kityIframe.postMessage({
                      type: 'save'
                  }, '*');
                }
              }
            ],
          });
        };

        var Dialog = { open: open };
    
        var register = function (editor) {
          editor.addCommand('KityformulaRule', function () {
            Dialog.open(editor);
          });
        };
        var Commands = { register: register };
    
        var register$1 = function (editor) {
          editor.addButton('kityformula', {
            icon: 'charmap',
            tooltip: 'Special character',
            cmd: 'KityformulaRule'
          });
          editor.addMenuItem('kityformula', {
            icon: 'charmap',
            text: 'Special character',
            cmd: 'KityformulaRule',
            context: 'insert'
          });
        };
        var Buttons = { register: register$1 };
    
        global.add('kityformula', function (editor) {
          Commands.register(editor);
          Buttons.register(editor);
        });
        function Plugin () {
        }

        return Plugin;    
    }());
})();
    